from django.db import models
from django.db.models.loading import get_app
from django.conf import settings
from django.contrib import admin
from homesite.bugs import globals
from django.core.urlresolvers import reverse

class Tag(models.Model):
  value = models.CharField(max_length=50)
  norm_value = models.CharField(max_length=50, editable=False)
  
  class Meta:
    ordering=('norm_value','value')
  
  def __unicode__(self):
    return self.value
    
  def save(self):
    tags = get_app('tagsfield')
    from tagsfield.utils import normalize_title

    self.norm_value = normalize_title(self.value)
    super(Tag, self).save()
    
  def get_absolute_url(self):
    if getattr(settings, 'TAGS_URL', ''):
      from urllib import quote
      try:
        return settings.TAGS_URL % quote(self.value)
      except TypeError:
        return settings.TAGS_URL + quote(self.value)
    else:
      #if getattr(globals,'project_id', ''):
      return reverse('homesite.bugs.views.tasktags', args=[globals.project_id, self.value])
          
      #else:
      #  return '2'

admin.site.register(Tag)